package com.bw.service.orders.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.entity.orders.OrderPicKey;
import com.bw.entity.orders.PiOrderPic;
import com.bw.mapper.orders.PiOrderPicMapper;
import com.bw.service.orders.IPiOrderPicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lizhengqian
 * @since 2020-06-19
 */
@Service
public class PiOrderPicServiceImpl extends ServiceImpl<PiOrderPicMapper, PiOrderPic> implements IPiOrderPicService {

    @Value("${filePath}")
    private String filePath;
    @Autowired
    private PiOrderPicMapper picMapper;

    @Transactional
    @Override
    public void importPic(MultipartFile file, Long code) throws IOException {
        String filename = file.getOriginalFilename();
        String itemCode = filename.substring(0, filename.lastIndexOf("."));
        String ext = filename.substring(filename.lastIndexOf("."), filename.length());
        String uuid= UUID.randomUUID().toString().replaceAll("-","");
        String dirPath=filePath+code+"/";
        File dirFile = new File(dirPath);
        if(!dirFile.exists()){
            dirFile.mkdir();
        }
        OrderPicKey orderPicKey = new OrderPicKey();
        orderPicKey.setItemId(code);
        orderPicKey.setCode(itemCode);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("itemId",code);
        queryWrapper.eq("code",itemCode);
        PiOrderPic Pic =  picMapper.selectOne(queryWrapper);

        PiOrderPic orderPic = new PiOrderPic();
        orderPic.setItemId(code);
        orderPic.setCode(itemCode);
        orderPic.setName(filename);
        orderPic.setFilePath(code+"/"+uuid+ext);
        orderPic.setFileSize(String.valueOf(file.getSize()));
        file.transferTo(new File(filePath+orderPic.getFilePath()));
        if(Pic!=null){
            File file1 = new File(filePath + Pic.getFilePath());
            file1.delete();
            picMapper.updateById(orderPic);
        }else{
            picMapper.insert(orderPic);
        }

    }

    @Override
    public InputStream preview(Long itemId, String code){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("itemId",itemId);
        queryWrapper.eq("code",code);
        PiOrderPic Pic =  picMapper.selectOne(queryWrapper);
        if(Pic!=null){
            try {
                FileInputStream stream = new FileInputStream(filePath + Pic.getFilePath());
                return stream;
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;
    }
}
